<template>
	<view>
		<j-cell-group>
			<j-cell title="序列号" :more="false" @jftap="jftapCopy">
				<view slot="tips" style="padding-right:20rpx;">
					<text class="SerialNo-text" v-if="device.deviceNo"
						style="">{{maskedStrings(device.deviceNo)}}</text>
					<uni-icons color="#FF0025" custom-prefix="iconfont" type="icon-xinxi-" size="24"></uni-icons>
				</view>
			</j-cell>
			<j-cell title="设备IP" v-if="showSwiper" :tips="DeviceIP" :more="false"></j-cell>
			<!-- 新门锁UI-lexin -->
			<j-cell title="信号强度" v-if="wsl" :tips="wsl" :more="false"></j-cell>
			<j-cell title="设备电量" v-if="bl" :tips="blChange+'%'" :more="false"></j-cell>
			<!--  -->
			<j-cell title="设备登录名" v-if="!showSwiper" :tips="device.deviceName" :more="false"></j-cell>
			<j-cell title="设备版本" class="lineFeed" :tips="info.HardWare" :more="false"></j-cell>
			<j-cell title="软件版本" class="lineFeed" :tips="info.SoftWareVersion" :more="false"></j-cell>
			<j-cell title="单片机版本" v-if="!showSwiper" :tips="McuVersion" :more="false"></j-cell>
			<j-cell title="发布状态" :tips="release" :more="false"></j-cell>
			<j-cell title="发布日期" :tips="info.BuildTime" :more="false"></j-cell>
			<j-cell title="设备时区" :tips="timeMin" :more="false"></j-cell>
			<!-- 新门锁UI -->
			<j-cell title="设备时间" v-if="deviceTypeStatus" :sub-title="' '" :more="false">
				<view slot="tips" style="padding-right:20rpx;">
					<view class="deviceTime-text" style="">{{deviceTime}}</view>
				</view>
			</j-cell>
			<j-cell title="网络模式" v-if="deviceTypeStatus" :tips="LoginMode" :more="false"></j-cell>
			<!-- 普通乐鑫 -->
			<j-cell v-if="device.localUiKey != 'ui_doorlock_home'" title="设备时间" :sub-title="showSwiper?'':'点击右侧同步设备时间'"
				@jftap="showSwiper?'':getDeviceStatus('synchTime')" :more="false">
				<view slot="tips" style="padding-right:20rpx;">
					<view class="deviceTime-text" style="">{{deviceTime}}</view>
				</view>
			</j-cell>
			<j-cell title="网络模式" v-if="showSwiper && device.localUiKey != 'ui_doorlock_home'" :tips="LoginMode"
				:more="false"></j-cell>
			<j-cell title="制造方式" v-if="!showSwiper" :tips="manufacturing" :more="false"></j-cell>
			<j-cell title="产品归属码" v-if="!showSwiper" :tips="PCMac" :more="false"></j-cell>
			<j-cell title="设备升级" v-if="deviceType&&deviceType=='AHD'" @jftap="checkUpdate(versionStatus)">
				<view v-if="versionStatus==1" slot="tips" class="cell-tips new">有新版本
					<image src="https://statics.xmcsrv.net/weixin/update.png" />
				</view>
				<view v-else-if="versionStatus==0" slot="tips" class="cell-tips">最新版本</view>
				<view v-else-if="versionStatus==-1" slot="tips" class="cell-tips">检查中...</view>
			</j-cell>
		</j-cell-group>
		<!-- bindtapReset -->
		<!--		<view v-if="!showSwiper" class="form-item">-->
		<!--			<button type="operation" @tap="getDeviceStatus('Reset')" style="margin-bottom: 10rpx ;"-->
		<!--				form-type="submit">恢复出厂设置</button>-->
		<!--			<button type="operation" @tap="getDeviceStatus('Restart')" form-type="submit">设备重启</button>-->
		<!--		</view>-->
		<j-modal :value="versionDialog" kText="确定" cancelText="一会再说" title="重要提示" @ok="onOk" @cancel="onCancel">
			<view slot="title" class="pop-title">
				升级过程中不能进行其他操作，请勿中途推出或按Home键。
				确认升级？
			</view>
		</j-modal>
		<j-modal :title="progress.title" :value="updateDialog" maskCloseAble="false" :showCancel="progress.status === 2"
			:showConfirm="progress.status !== 0" :okText="progress.status === 2 ? '重试' : '完成'" cancelText="取消"
			@cancel="cancelFn" @ok="okFn">
			<view slot="content" class="progress-box">
				<view>
					<j-progress boxWidth="100" boxHeight="100" lineWidth="3"
						:bgColor="progress.status!==2?'#B9E8E8':'#f49590'" :fontShow="progress.fontShow"
						:progressColor="progress.status!==2?'#FF0025':'#f49590'" fontColor="#5677fc" fontSize="14"
						:percent="progress.percent">
						<image src="https://statics.xmcsrv.net/weixin/cha.png" class="cha"
							v-if="progress.status === 2" />
					</j-progress>

				</view>
				<view>
					<text v-if="progress.status===0">升级过程中不能进行其他操作，请勿中途退出或按Home键</text>
					<text v-else-if="progress.status===2" style="color: #f49590">失败原因：升级数据错误</text>
					<text v-else-if="progress.status===1">设备重启中，请稍后访问</text>
				</view>

			</view>
		</j-modal>
	</view>
</template>

<script>
	import getDateTime from '@/jvss/pagesDevice/utils/getdateTime.js';
	let constant = getApp().globalData.constant;
	let EE_DEV_SMARTDOORLOCK = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK;
	let EE_DEV_SMARTDOORLOCK2 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK2;
	let EE_DEV_SMARTDOORLOCK3 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK3;
	let EE_DEV_SMARTDOORLOCK4 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK4;
	let EE_DEV_SMARTDOORLOCK5 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK5;
	let EE_DEV_SMARTDOORLOCK6 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK6;
	let EE_DEV_SMARTDOORLOCK7 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK7;
	let EE_DEV_SMARTDOORLOCK8 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK8;
	let EE_DEV_SMARTDOORLOCK9 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK9;
	let EE_DEV_SMARTDOORLOCK10 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK10;
	let EE_DEV_SMARTDOORLOCK11 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK11;
	let EE_DEV_SMARTDOORLOCK12 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK12;
	let EE_DEV_SMARTDOORLOCK14 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK14;
	let EE_DEV_SMARTDOORLOCK22 = constant.DEVICE_TYPE.EE_DEV_SMARTDOORLOCK22;
	const {
		date2FmtStr,
		HexString2Bytes,
		getStatus,
		convertdBm2RSSI,
		maskedString
	} = require('@/utils/util.js');
	export default {
		computed: {
			showSwiper() {
				return this.device.parentPid === EE_DEV_SMARTDOORLOCK4 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK5 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK6 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK7 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK10 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK11 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK12 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK14
			},
			deviceTypeStatus() {
				return this.deviceStatus != 'deep' &&
					this.deviceStatus != 'OffLine' &&
					this.device.localUiKey == 'ui_doorlock_home'
			},
            blChange() {
                if(this.bl>=100) return 100
                else if(this.bl<=0) return 0
                else return this.bl
            }
		},
		data() {
			return {
				device: {},
				info: {
					SerialNo: '',
					HardWare: '',
					HardWareVersion: '',
					SoftWareVersion: '',
					EncryptVersion: '',
					BuildTime: '',
					UpdataTime: ''
				},
				wsl: "", //信号强度
				bl: "", //设备电量
				LoginMode: '', //网络模式
				deviceTime: '', //设备时间
				McuVersion: '', //单片机版本
				PCMac: '', //产品归属码
				manufacturing: '', //制造方式
				timeMin: '', //设备时区
				nowTime: '',
				release: '',
				DeviceIP: "",
				networkModel: '',
				timer: null,
				GeneralLocation: '', //普通配置
				deviceType: '',
				versionStatus: -1,
				version: {},
				versionDialog: false,
				updateDialog: false,
				newVersion: {},
				progress: {
					ret: 0,
					title: '正在升级中',
					status: 0, // 升级状态,0 升级中，1 成功， 2 失败
					fontShow: true,
					percent: 0,
				},
				deviceStatus: null, //设备状态
			}
		},
		onLoad(opt) {
			let enData = decodeURIComponent(opt.device)
			this.device = JSON.parse(enData)
			console.log('关于设备', this.device);
			this.deviceType = opt.deviceType
			this.deviceStatus = getStatus(this.device.status);
			if (this.device.localUiKey != 'ui_doorlock_home') {
				this.getCacheData();
			}
			// if (this.device.localUiKey == 'ui_doorlock_home') {
			// 	this.getTailoredConfig(); //设备深度休眠或者离线使用离线设置
			// }
		},
		onShow() {
			//SystemInfoEx 获取单片机版本
			//General.Location 本地化配置；
			//Camera.FishLensParam产品地归属码  
			let deviceStatus = this.deviceStatus;
			let cacheData = uni.getStorageSync(this.device.deviceNo);
			if (deviceStatus == 'deep' || deviceStatus == 'OffLine') { //深度休眠需要手动唤醒
				if (this.device.localUiKey == 'ui_doorlock_home') {
					this.getTailoredConfig(); //设备深度休眠或者离线使用离线设置
				} else {
					if (!cacheData.info) {
						this.init()
					}
				}
			} else {
				this.init()
				this.getDevicePropStatus()
				// if(this.device.localUiKey == 'ui_doorlock_home'){
				// 	this.getTailoredConfig(); //设备深度休眠或者离线使用离线设置
				// }
			}
			// let status = this.device.status
			// if (status.status === 'online') {
			// 	if (status.wakeUpStatus === '1' || (status.wakeUpStatus === '0' && status.wakeUpEnable ===
			// 		'1')) { //设备已唤醒或者休眠支持远程唤醒
			// 		this.init()
			// 	} else if (!(status.wakeUpStatus === '0' && status.wakeUpEnable === '0')) { //深度休眠需要手动唤醒
			// 		let cacheData = uni.getStorageSync(this.device.deviceNo)
			// 		if (!cacheData.info) {
			// 			this.init()
			// 		} else {
			// 			// let date = new Date()
			// 			// let time = date.getTime()
			// 			// setInterval(() => {
			// 			// 	time = time + 1000
			// 			// 	this.deviceTime = date2FmtStr(new Date(time))
			// 			// }, 1000)
			// 		}
			// 	}
			// }
		},
		methods: {
			maskedStrings(e) {
				return maskedString(e)
			},
			getDevicePropStatus(e) {
				let data = {
					"sn": this.device.deviceNo,
					"props": ['wifiSignal', 'batteryPower']
				};
				getApp().globalData.api.getDevicePropStatus(data, (result) => {
					console.log(result, 'jvss获取的设备信息')
					this.wsl = convertdBm2RSSI(result.wifiSignal, '', 'level')
					this.bl = result.batteryPower ? result.batteryPower : 0
				});
			},
			getTailoredConfig(ev, type) {
				uni.showLoading({
					title: '加载中...',
					mask: true
				})
				let token = this.device.token
				let data = {
					"sn": this.device.deviceNo,
					"configs": []
				};
				getApp().globalData.api.getTailoredConfig(data, token, (result) => {
					uni.hideLoading();
					this.DeviceIP = result.devip; //设备IP
					this.wsl = result.wsl; //信号强度
					this.bl = result.bl ? result.bl > 0 ? result.bl : '' : ""; //设备电量
					this.info.HardWare = result.devv; //发布版本
					this.info.SoftWareVersion = result.swv; //软件版本
					this.release = result.rsta; //发布状态
					this.info.BuildTime = result.swrt; //发布日期
					this.timeMin = result.devtz; //设备时区
					console.log(result, '离线设置获取');
				});
			},
			// 初始化progress
			initProgress() {
				this.progress = {
					ret: 0,
					title: '正在升级中',
					status: 0, // 升级状态,0 升级中，1 成功， 2 失败
					fontShow: true,
					percent: 0,
				}
				this.updateStatus = false
			},
			onOk() {
				// console.log('开始升级')
				this.getLogin((res) => {
					// 更新版本
					this.updateVersion()
				})
				this.versionDialog = false
				this.updateDialog = true
			},
			onCancel() {
				console.log('取消升级')
				this.versionDialog = false
			},
			// 升级取消
			cancelFn() {
				this.updateDialog = false
				this.initProgress()
				console.log('关闭弹窗')
			},
			// 低功耗设备唤醒
			wakeup(callback, decode = false, simple = false) {
				let data = {
					sn: this.device.deviceNo,
					hostedControl: "1", //是否唤醒主控（空值或0：不唤醒主控；1：唤醒主控）
				}
				getApp().globalData.api.wakeup(data, this.device.token, (result) => {
					callback(result);
				}, decode, simple);
			},
			// 升级确定
			okFn() {
				const status = this.progress.status
				if (status === 2) {
					// 升级失败，重试
					if (this.updateTimer) {
						clearTimeout(this.updateTimer)
					}
					this.getLogin((res) => {
						// this.updateVersion(1)
					})
					console.log('重试升级')
				} else if (status === 1) {
					// 升级完成，关闭弹窗, 重置数据
					// 升级完成，返回首页
					uni.switchTab({
						url: '/jvss/pages/device/home'
					});
					console.log(123)
				}
			},
			// 检查更新
			checkUpdate(versionStatus) {
				if (versionStatus == 1) {
					console.log('需要更新')
					this.versionDialog = true
				} else if (versionStatus == 0) {
					uni.showToast({
						title: '已经是最新版本',
						icon: 'none'
					})
					console.log('不需要更新')
				}
			},
			// 获取设备版本
			getVersion() {
				let token = this.device.token
				console.log(this.device, '获取设备版本')
				const SN = this.device.deviceNo
				let DevID = this.info.SoftWareVersion.split('.').join('')
				DevID = DevID.slice(7, )
				console.log(DevID, this.info)
				const CurVersion = this.info.BuildTime.slice(0, 10)
				const DevType = DevID.slice(0, 3) === 'IOT' ? 'IOT' : 'IPC'
				const data = {
					Name: 'OPVersionReq',
					OPVersionReq: {
						Expect: 0,
						Language: "Chinese",
						Manual: 1,
						SN,
						DevID,
						CurVersion,
						DevType,
						// UUID: "MoblieAPP"
						UUID: SN,
					}
				}
				this.version = {
					...data
				}
				console.log(data)
				console.log(this.info.BuildTime.slice(0, 10))
				getApp().globalData.api.opdev(data, token, (res) => {
					console.log(res, '获取版本');
					if (res.data.code == 2000) {
						// 请求成功
						if (res.data.data.Ret == 100) {
							console.log(res.data.data, '设备版本')
							// 拿到版本信息
							let result = res.data.data
							console.log(Object.keys(result.OPVersionRep))
							if (!Object.keys(result.OPVersionRep)) {
								// 无可用版本，表示为最新版本
								// console.log('无可用版本，表示为最新版本')
								this.versionStatus = 0
							} else {
								// 点击即可升级
								// console.log('点击即可升级')
								this.versionStatus = 1
								// 存储可升级版本信息
								this.newVersion = {
									verDate: result.OPVersionRep.Date,
									FileName: result.OPVersionRep.FileName,
								}
							}
						} else if (res.data.data.Ret == 527) {
							//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
							this.wakeup(() => {
								this.getVersion()
							})
						} else {
							// 获取版本失败
							console.log(res.data.data.RetMsg)
						}

					} else {
						// 请求失败
					}

				}, false, false)


			},
			// 开始升级
			updateVersion(status) {
				let token = this.device.token
				const {
					Manual,
					SN,
					DevID
				} = this.version.OPVersionReq
				const {
					verDate,
					FileName
				} = this.newVersion
				const data = {
					Name: 'OPStartUpgradeReq',
					OPStartUpgradeReq: {
						Manual,
						UUID: SN,
						DevID,
						verDate,
						FileName,
					}
				}
				if (status) {
					this.progress.status = 0
					this.progress.title = '正在升级中'
					this.progress.fontShow = true
					this.progress.percent = 0
					this.progress.ret = 0
				}
				console.log(data, '传递的数据')

				this.getDeviceVersion(data, token, (res) => {
					console.log(res, '开始升级设备')
					if (res.data.code === 2000) {
						// 请求成功，改变状态，获取升级进度
						this.progress.status = 0
						this.updateStatus = true
						// this.updateTimer = setInterval(() => {
						//     this.getUpgradeProgress()
						// }, 1000)
						this.getUpgradeProgress()
					} else {
						// 升级失败
						// this.progress
						this.progress.status = 2
						this.progress.title = '升级失败'
						this.progress.fontShow = false
						this.progress.percent = 0
						console.log(this.progress)
					}
				}, false, false)
			},
			// 获取升级进度
			getUpgradeProgress() {

				let token = this.device.token
				let data = {
					Sn: this.version.OPVersionReq.SN
				}
				getApp().globalData.api.getUpgradeProgress(data, token, (res) => {
					// if(res.code === 2000) {
					if (res.data.code !== 2000) {
						// 更新失败
						this.progress.status = 2
						this.progress.title = '升级失败'
						this.progress.fontShow = false
						this.progress.percent = 0
						// clearInterval(this.updateTimer)
						return
					} else {
						// console.log(res,  '当前升级进度')
						// console.log(res.data,  '当前升级进度1')
						if (res.data.data.Ret === 515) {
							// 升级成功
							this.progress.status = 1
							this.progress.title = '升级成功'
							this.progress.fontShow = true
							this.progress.percent = 100
							// clearInterval(this.updateTimer)
							return
						} else {
							// 正在升级
							this.getUpgradeProgress()
							this.progress.percent = res.data.data.Ret
						}
						console.log(this.progress.ret)
					}
					console.log(res, '获取升级进度111')
				}, false, false)
			},
			init() {
				if (this.device.pid == 'HAVLS079803D900N') {
					setTimeout(() => {
						this.getinfo();
					}, 2000)
				} else {
					this.getinfo();
				}
				this.OPTimeQuery() //获取系统时间
				this.queryDevice() //制造方式
				this.getTimeZone();
				this.getLoginNetworkMode() //网络模式
				if (this.device.parentPid === EE_DEV_SMARTDOORLOCK11 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK5 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK6 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK7 ||
					this.device.parentPid === EE_DEV_SMARTDOORLOCK22) {} else {
					this.SystemInfoEx() //单片机版本
					this.getLocation() //普通配置
					this.FishLensParam() //产品归属地
				}
			},
			getLoginNetworkMode() {
				let data = {
					sn: this.device.deviceNo
				};
				let token = this.device.token;
				getApp().globalData.api.getLoginNetworkMode(data, token, (result) => {
					console.log('获取设备能力集:', result);
					this.LoginMode = result.LoginMode;
					this.setCacheData('LoginMode', this.LoginMode)
				});
			},
			// 缓存数据
			setCacheData(param, value, type) {
				let cacheData = uni.getStorageSync(this.device.deviceNo)
				if (cacheData) {
					if (type == 1) { //基础信息
						cacheData[param] = value
						uni.setStorageSync(this.device.deviceNo, cacheData)
					} else {
						// cacheData['extra'][param] = value
						cacheData[param] = value
						uni.setStorageSync(this.device.deviceNo, cacheData)
					}
				} else {
					if (type == 1) {
						uni.setStorageSync(this.device.deviceNo, {
							info: value
						})
					} else {
						let extra = {}
						extra[param] = value
						uni.setStorageSync(this.device.deviceNo, extra)
					}
				}
			},
			//获取缓存数据
			getCacheData() {
				let cacheData = uni.getStorageSync(this.device.deviceNo)
				if (cacheData) {
					if (cacheData.info) {
						this.info = cacheData.info
					}
					this.McuVersion = cacheData.McuVersion || ''
					this.PCMac = cacheData.PCMac || ''
					this.manufacturing = cacheData.manufacturing || ''
					this.timeMin = cacheData.timeMin || ''
					this.release = cacheData.release || ''
					this.DeviceIP = cacheData.DeviceIP || ''
				}
			},
			// 获取设备状态
			getDeviceStatus(type) {
				const data = {
					token: [this.device.token]
				};
				getApp().globalData.api.getDeviceStatus(data, (result) => {
					let item = result[0]
					if (item.status === 'online') {
						if (item.wakeUpStatus === '0') {
							if (item.wakeUpEnable === '0') {
								//深度休眠 手动唤醒
								uni.showToast({
									title: '设备处于深度休眠，请手动唤醒',
									icon: 'none'
								})
							} else {
								//浅休眠 可以唤醒
								if (type == 'synchTime') {
									this.synchTime()
								} else {
									this.bindtapReset(type)
								}
							}
						} else if (item.wakeUpStatus === '1') {
							//已唤醒 在线
							if (type == 'synchTime') {
								this.synchTime()
							} else {
								this.bindtapReset(type)
							}
						} else if (item.wakeUpStatus === '2') {
							// 准备休眠中
							uni.showToast({
								title: '设备休眠中，请稍后唤醒',
								icon: 'none'
							})
						}
					} else {
						//离线
						uni.showToast({
							title: '设备已离线',
							icon: 'none'
						})
					}
				})
			},
			// 获取升级版本
			getDeviceVersion(data, token, callback, decode = false, simple = true) {
				getApp().globalData.api.opdev(data, token, (res) => {
					callback(res)
				}, decode, simple)
			},
			// 登陆设备
			getLogin(callback) {
				uni.showLoading({
					mask: true,
				});
				let token = this.device.token;
				let data = {
					encryptType: 'DISABLE',
					LoginType: 'DVRIP-Web',
					UserName: 'admin',
					PassWord: this.device.devPassWord,
					Name: 'generalinfo'
				};
				if (this.device.adminToken) {
					data.LoginToken = this.device.adminToken
					data.encryptType = 'TOKEN'
				}
				getApp().globalData.api.deviceLogin(data, token, (result) => {
					uni.hideLoading();
					if (result.data.code === 2000) {
						// 登录成功 或 已登录
						if (result.data.data.Ret === 100 || result.data.data.Ret === 104) {
							console.log('成功')
							callback()
						} // 出错
						else {
							uni.showToast({
								icon: 'none',
								title: getApp().globalData.errCodeMsg.RetCodeMsg[result.data.data.Ret] ||
									result.data.data.RetMsg,
							});
						}
					} else {
						uni.showToast({
							icon: 'none',
							title: getApp().globalData.errCodeMsg.CodeMsg[result.data.code] || result.data
								.msg
						});
					}
				})
			},
			// 获取关于设备
			getinfo() {
				uni.showLoading({
					title: '获取设备基础信息...'
				});
				let token = this.device.token;
				const data = {
					Name: 'SystemInfo'
				};
				getApp().globalData.api.getinfo(data, token, (result) => {
					console.log('获取设备基础信息:', result, this.showSwiper);
					uni.hideLoading()
					if (result.data.data.Ret == 100) {
						if (result.data.data.SystemInfo.DeviceIP) {
							let arr = HexString2Bytes(result.data.data.SystemInfo.DeviceIP)
							this.DeviceIP = arr[4] + '.' + arr[3] + '.' + arr[2] + '.' + arr[1]
						}
						this.info = result.data.data.SystemInfo
						this.findRelease();
						this.setCacheData('info', this.info, 1)
						this.setCacheData('DeviceIP', this.DeviceIP)
					} else if (result.data.data.Ret == 527) {
						//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
						this.wakeup(() => {
							this.getinfo()
						})
					}
					// 关闭获取版本入口
					// this.getVersion()
				}, false, false);
			},
			queryDevice() {
				let token = this.device.token;
				const data = {
					sn: this.device.deviceNo
				};
				getApp().globalData.api.queryDevice(data, token, (result) => {
					// console.log('制造方式:', result);
					if (result.device.chipOemId <= 8) {
						this.manufacturing = 'original(' + result.device.chipOemId + ')'
					} else {
						this.manufacturing = 'licensee(' + result.device.chipOemId + ')'
					}
					this.setCacheData('manufacturing', this.manufacturing)
				});
			},
			// 发布状态
			findRelease() {
				const data = {
					devId: this.device.deviceNo,
					releaseDate: this.info.BuildTime
				};
				getApp().globalData.api.getfindRelease(data, (result) => {
					console.log('发布状态:', result);
					if (result.release) {
						this.release = 'Release'
					} else {
						this.release = 'Debug'
					}
					this.setCacheData('release', this.release)
				});
			},
			//获取设备语言
			getLocation() {
				this.getconfig('General.Location')
			},
			// 产地归属码
			FishLensParam() {
				this.getconfig('Camera.FishLensParam')
			},
			// 获取时区
			getTimeZone() {
				this.getconfig('System.TimeZone')
			},
			getconfig(v) {
				uni.showLoading();
				let token = this.device.token;
				let data = {
					Name: v,
				};
				getApp().globalData.api.getconfig(data, token, (result) => {
					console.log(result, 'getconfig')
					uni.hideLoading()
					if (result.Ret == 100) {
						if (v == 'System.TimeZone') {
							let sign = result['System.TimeZone'].timeMin
							console.log(sign)
							this.timeMin = sign > 0 ? '西' + result['System.TimeZone'].timeMin / 60 : '东' + Math
								.abs(
									result['System.TimeZone'].timeMin / 60)
							this.setCacheData('timeMin', this.timeMin)
						} else if (v == 'Camera.FishLensParam') {
							this.PCMac = result['Camera.FishLensParam'][0].PCMac
							this.setCacheData('PCMac', this.PCMac)
						} else if (v == 'General.Location') {
							console.log(result, 'General.Location')
							this.GeneralLocation = result['General.Location']
						}
					} else if (result.Ret == 527) {
						//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
						this.wakeup(() => {
							this.getconfig(v)
						})
					}
				});
			},
			// 设置同步时间
			// 1.同步时区->同步夏令时->同步时间
			// 同步时区
			synchTime2() {
				this.getDeviceStatus()
			},
			synchTime() {
				uni.showLoading();
				var offset_GMT = new Date().getTimezoneOffset();
				let token = this.device.token;
				let data = {
					Name: 'System.TimeZone',
					"System.TimeZone": {
						"timeMin": offset_GMT
					}
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.hideLoading()
					console.log(result)
					if (result.Ret == 100) {
						this.savingTime()
					}
				});
			},
			// 2.同步夏令时
			savingTime() {
				uni.showLoading();
				let token = this.device.token;
				let data = {
					Name: 'General.Location',
					"General.Location": {
						"DSTEnd": this.GeneralLocation.DSTEnd,
						"DSTRule": this.GeneralLocation.DSTRule,
						"DSTStart": this.GeneralLocation.DSTStart
					}
				};
				getApp().globalData.api.setconfig(data, token, (result) => {
					uni.hideLoading()
					console.log(result)
					if (result.Ret == 100) {
						this.OPTimeSetting()
					}
				});
			},
			// 3.同步时间
			OPTimeSetting() {
				uni.showLoading();
				let token = this.device.token;
				let timeStr = getDateTime.dateTimeStr('y-m-d h:i:s'); // y:年 m:月 d:日 h:时 i:分 s:秒 中间的分割符号可更改
				console.log(timeStr, '当前时间')
				let data = {
					// Name: "OPUTCTimeSetting",
					Name: "OPTimeSetting",
					OPTimeSetting: timeStr
				};
				// getconfig
				getApp().globalData.api.opdev(data, token, (result) => {
					uni.hideLoading();
					if (result.Ret == 100) {
						setTimeout(() => {
							uni.showToast({
								title: '同步成功',
								icon: 'none'
							});
						}, 100)
						this.OPTimeQuery();
						this.getTimeZone();
					} else if (result.Ret == 527) {
						//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
						this.wakeup(() => {
							this.OPTimeSetting()
						})
					}
					// console.log(result);
				});
			},
			// 单片机版本
			SystemInfoEx() {
				let token = this.device.token;
				const data = {
					Name: 'SystemInfoEx'
				};
				getApp().globalData.api.getinfo(data, token, (result) => {
					console.log(result, '单片机版本');
					if (result.data.data.Ret == 100) {
						this.McuVersion = result.data.data.SystemInfoEx.McuVersion
						this.setCacheData('McuVersion', this.McuVersion)
					} else if (result.data.data.Ret == 527) {
						//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
						this.wakeup(() => {
							this.getinfo()
						})
					}
				}, false, false);
			},
			// 复制序列号
			jftapCopy() {
				uni.setClipboardData({
					data: this.info.SerialNo,
					success: function() {
						// console.log('success');
					}
				})
			},
			// 获取系统时间
			OPTimeQuery() {
				let token = this.device.token;
				let data = {
					Name: 'OPTimeQuery',
				};
				if (this.timer) clearInterval(this.timer)
				getApp().globalData.api.opdev(data, token, (result) => {
					console.log(result, '系统时间');
					if(result.Ret ==100){
						this.deviceTime = null
						this.deviceTime = result.OPTimeQuery;
						var format = result.OPTimeQuery.replace(/-/g, '/');
						let date = new Date(format);
						let time = date.getTime();
						this.timer = setInterval(() => {
							time = time + 1000
							this.deviceTime = date2FmtStr(new Date(time))
						}, 1000)
					}else if (result.Ret == 527) {
						//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
						this.wakeup(() => {
							this.OPTimeQuery()
						})
					}
				});
			},
			// 恢复出厂设置 重新启动
			bindtapReset(e) {
				let token = this.device.token;
				let data = {
					Name: 'OPDefaultConfig',
					OPDefaultConfig: {
						Account: true,
						Alarm: true,
						CommPtz: true,
						Encode: true,
						General: true,
						NetCommon: true,
						NetServer: true,
						Preview: true,
						Record: true,
						XMModeSwitch: true,
						CameraPARAM: true
					}
				};
				let title = '确认恢复出厂设置？'
				let loadingtitle = '恢复中...'
				let successtitle = '恢复成功'
				if (e == 'Restart') {
					data = {
						Name: 'OPMachine',
						OPMachine: {
							Action: 'Reboot'
						}
					};
					title = '确认重启设备？'
					loadingtitle = '重启中...'
					successtitle = '重启成功'
				}
				uni.showModal({
					title: '提示',
					content: title,
					success: (res) => {
						if (res.confirm) {
							uni.showLoading({
								title: loadingtitle
							});
							getApp().globalData.api.opdev(data, token, (result) => {
								uni.hideLoading()
								console.log(result);
								if (result.Ret === 100) {
									if (e == 'Reset') {
										getApp().globalData.mqtt.getUnsubscribe(this.device.deviceNo,
											'device')
									}
									uni.showToast({
										title: successtitle,
										duration: 2000
									});
									setTimeout(() => {
										uni.switchTab({
											url: '/jvss/pages/device/home'
										});
									}, 1000)
								} else if (result.Ret == 527) {
									//消息发给主控失败了，需要重新唤醒主控（650+3861L门锁用）
									this.wakeup(() => {
										this.bindtapReset(e)
									})
								}
							});
						}
					}
				});
			},
		}
	}
</script>

<style lang="scss">
	.SerialNo-text {
		color: #999999;
		font-size: 24rpx;
	}

	.deviceTime-text {
		font-size: 24rpx;
		color: #FF0025;
	}

	.cell-tips {
		font-size: 24rpx;
		color: #999999;
		max-width: 62vw;
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	.new {
		color: #FF0025;
		display: flex;
		align-items: center; //对齐
		justify-content: center; //居中

		image {
			width: 36rpx;
			height: 36rpx;
			margin-left: -12rpx;
		}
	}

	.pop-title {
		color: #999999;
		width: 94%;
		margin: 0 auto;
		padding-top: 40rpx;
		font-size: 26.875rpx;
	}

	.circular-container {
		display: flex;
		justify-content: center;
		align-items: center;
	}

	.progress-box {
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
	}

	.cha {
		width: 60rpx;
		height: 60rpx;
		position: absolute;
	}

	.lineFeed {
		/deep/.cell-tips {
			word-wrap: break-word;
			/*允许长单词换行到下一行*/
			word-break: break-all;
			/*这个参数根据需要来决定要不要*/
			overflow: hidden;
			/*这个参数根据需要来决定要不要*/
			white-space: break-spaces;
			text-align: end;
		}
	}
</style>